<template>
  <div>
    <el-card class="card-h-80">
      <!-- <div slot="header" class="clearfix">
        <span>待办任务</span>
      </div> -->
      <el-tabs v-model="categories">
        <el-tab-pane v-if="categoriesCodes.includes('sl')" label="总行待办任务" name="sl">
          <el-tabs v-model="show2">
            <el-tab-pane v-if="isMyTab('RukuSL')" key="RukuSL" label="物资入库" name="RukuSL">
              <span slot="label">
                物资入库
                <el-badge v-if="todoNum('RukuSL') > 0" :value="todoNum('RukuSL')" size="mini" class="item" />
              </span>
              <RukuSL v-if="show2 === 'RukuSL'" work-order-request="todo" work-order-type="RukuSL" @reload="refresh" />
            </el-tab-pane>
            <el-tab-pane v-if="isMyTab('LingyongSL')" key="LingyongSL" label="物资领用" name="LingyongSL">
              <span slot="label">
                物资领用
                <el-badge v-if="todoNum('LingyongSL') > 0" :value="todoNum('LingyongSL')" size="mini" class="item" />
              </span>
              <LingyongSL v-if="show2 === 'LingyongSL'" work-order-request="todo" work-order-type="LingyongSL" @reload="refresh" />
            </el-tab-pane>
            <el-tab-pane v-if="isMyTab('PeifaSL')" key="PeifaSL" label="物资配发" name="PeifaSL">
              <span slot="label">
                物资配发
                <el-badge v-if="todoNum('PeifaSL') > 0" :value="todoNum('PeifaSL')" size="mini" class="item" />
              </span>
              <PeifaSL v-if="show2 === 'PeifaSL'" work-order-request="todo" work-order-type="PeifaSL" @reload="refresh" />
            </el-tab-pane>
            <el-tab-pane v-if="isMyTab('ZhuanyiSL')" key="ZhuanyiSL" label="物资转移" name="ZhuanyiSL">
              <span slot="label">
                物资转移
                <el-badge v-if="todoNum('ZhuanyiSL') > 0" :value="todoNum('ZhuanyiSL')" size="mini" class="item" />
              </span>
              <ZhuanyiSL v-if="show2 === 'ZhuanyiSL'" work-order-request="todo" work-order-type="ZhuanyiSL" @reload="refresh" />
            </el-tab-pane>
            <el-tab-pane v-if="isMyTab('WeixiuSL')" key="WeixiuSL" label="物资维修" name="WeixiuSL">
              <span slot="label">
                物资维修
                <el-badge v-if="todoNum('WeixiuSL') > 0" :value="todoNum('WeixiuSL')" size="mini" class="item" />
              </span>
              <WeixiuSL v-if="show2 === 'WeixiuSL'" work-order-request="todo" work-order-type="WeixiuSL" @reload="refresh" />
            </el-tab-pane>
            <el-tab-pane v-if="isMyTab('JieyongSL')" key="JieyongSL" label="物资借用" name="JieyongSL">
              <span slot="label">
                物资借用
                <el-badge v-if="todoNum('JieyongSL') > 0" :value="todoNum('JieyongSL')" size="mini" class="item" />
              </span>
              <JieyongSL v-if="show2 === 'JieyongSL'" work-order-request="todo" work-order-type="JieyongSL" @reload="refresh" />
            </el-tab-pane>
            <el-tab-pane v-if="isMyTab('GuihuanSL')" key="GuihuanSL" label="物资归还" name="GuihuanSL">
              <span slot="label">
                物资归还
                <el-badge v-if="todoNum('GuihuanSL') > 0" :value="todoNum('GuihuanSL')" size="mini" class="item" />
              </span>
              <GuihuanSL v-if="show2 === 'GuihuanSL'" work-order-request="todo" work-order-type="GuihuanSL" @reload="refresh" />
            </el-tab-pane>
            <el-tab-pane v-if="isMyTab('TuikuSL')" key="TuikuSL" label="物资退库" name="TuikuSL">
              <span slot="label">
                物资退库
                <el-badge v-if="todoNum('TuikuSL') > 0" :value="todoNum('TuikuSL')" size="mini" class="item" />
              </span>
              <TuikuSL v-if="show2 === 'TuikuSL'" work-order-request="todo" work-order-type="TuikuSL" @reload="refresh" />
            </el-tab-pane>
            <el-tab-pane v-if="isMyTab('DiaoboSL')" key="DiaoboSL" label="物资调拨" name="DiaoboSL">
              <span slot="label">
                物资调拨
                <el-badge v-if="todoNum('DiaoboSL') > 0" :value="todoNum('DiaoboSL')" size="mini" class="item" />
              </span>
              <DiaoboSL v-if="show2 === 'DiaoboSL'" work-order-request="todo" work-order-type="DiaoboSL" @reload="refresh" />
            </el-tab-pane>
            <el-tab-pane v-if="isMyTab('BaofeiSL')" key="BaofeiSL" label="物资报废" name="BaofeiSL">
              <span slot="label">
                物资报废
                <el-badge v-if="todoNum('BaofeiSL') > 0" :value="todoNum('BaofeiSL')" size="mini" class="item" />
              </span>
              <BaofeiSL v-if="show2 === 'BaofeiSL'" work-order-request="todo" work-order-type="BaofeiSL" @reload="refresh" />
            </el-tab-pane>
            <el-tab-pane v-if="isMyTab('RukuFinanceSL')" key="RukuFinanceSL" label="物资报废" name="RukuFinanceSL">
              <span slot="label">
                财管资产
                <el-badge v-if="todoNum('RukuFinanceSL') > 0" :value="todoNum('RukuFinanceSL')" size="mini" class="item" />
              </span>
              <RukuFinanceSL v-if="show2 === 'RukuFinanceSL'" work-order-request="todo" work-order-type="RukuFinanceSL" @reload="refresh" />
            </el-tab-pane>
          </el-tabs>
        </el-tab-pane>
        <el-tab-pane v-if="categoriesCodes.includes('kj')" label="信息技术中心待办任务" name="kj">
          <el-tabs v-model="show">
            <el-tab-pane v-if="isMyTab('RukuKJ')" key="RukuKJ" label="物资入库" name="RukuKJ">
              <span slot="label">
                物资入库
                <el-badge v-if="todoNum('RukuKJ') > 0" :value="todoNum('RukuKJ')" size="mini" class="item" />
              </span>
              <RukuKJ v-if="show === 'RukuKJ'" work-order-request="todo" work-order-type="RukuKJ" @reload="refresh" />
            </el-tab-pane>

            <el-tab-pane v-if="isMyTab('LingyongKJ')" key="LingyongKJ" label="物资领用" name="LingyongKJ">
              <span slot="label">
                物资领用
                <el-badge v-if="todoNum('LingyongKJ') > 0" :value="todoNum('LingyongKJ')" size="mini" class="item" />
              </span>
              <LingyongKJ v-if="show === 'LingyongKJ'" work-order-request="todo" work-order-type="LingyongKJ" @reload="refresh" />
            </el-tab-pane>

            <el-tab-pane v-if="isMyTab('ZhuanyiKJ')" key="ZhuanyiKJ" label="物资转移" name="ZhuanyiKJ">
              <span slot="label">
                物资转移
                <el-badge v-if="todoNum('ZhuanyiKJ') > 0" :value="todoNum('ZhuanyiKJ')" size="mini" class="item" />
              </span>
              <ZhuanyiKJ v-if="show === 'ZhuanyiKJ'" work-order-request="todo" work-order-type="ZhuanyiKJ" @reload="refresh" />
            </el-tab-pane>

            <el-tab-pane v-if="isMyTab('WeixiuKJ')" key="WeixiuKJ" label="物资维修" name="WeixiuKJ">
              <span slot="label">
                物资维修
                <el-badge v-if="todoNum('WeixiuKJ') > 0" :value="todoNum('WeixiuKJ')" size="mini" class="item" />
              </span>
              <WeixiuKJ v-if="show === 'WeixiuKJ'" work-order-request="todo" work-order-type="WeixiuKJ" @reload="refresh" />
            </el-tab-pane>

            <el-tab-pane v-if="isMyTab('TuikuKJ')" key="TuikuKJ" label="物资退库" name="TuikuKJ">
              <span slot="label">
                物资退库
                <el-badge v-if="todoNum('TuikuKJ') > 0" :value="todoNum('TuikuKJ')" size="mini" class="item" />
              </span>
              <TuikuKJ v-if="show === 'TuikuKJ'" work-order-request="todo" work-order-type="TuikuKJ" @reload="refresh" />
            </el-tab-pane>

            <el-tab-pane v-if="isMyTab('BaofeiKJ')" key="BaofeiKJ" label="物资报废" name="BaofeiKJ">
              <span slot="label">
                物资报废
                <el-badge v-if="todoNum('BaofeiKJ') > 0" :value="todoNum('BaofeiKJ')" size="mini" class="item" />
              </span>
              <BaofeiKJ v-if="show === 'BaofeiKJ'" work-order-request="todo" work-order-type="BaofeiKJ" @reload="refresh" />
            </el-tab-pane>
            <el-tab-pane v-if="isMyTab('DiaoboKJ')" key="DiaoboKJ" label="物资调拨" name="DiaoboKJ">
              <span slot="label">
                物资调拨
                <el-badge v-if="todoNum('DiaoboKJ') > 0" :value="todoNum('DiaoboKJ')" size="mini" class="item" />
              </span>
              <DiaoboKJ v-if="show === 'DiaoboKJ'" work-order-request="todo" work-order-type="DiaoboKJ" @reload="refresh" />
            </el-tab-pane>

            <el-tab-pane v-if="isMyTab('RukuDZKJ')" key="RukuDZKJ" label="易耗入库" name="RukuDZKJ">
              <span slot="label">
                易耗入库
                <el-badge v-if="todoNum('RukuDZKJ') > 0" :value="todoNum('RukuDZKJ')" size="mini" class="item" />
              </span>
              <RukuDZKJ v-if="show === 'RukuDZKJ'" work-order-request="todo" work-order-type="RukuDZKJ" @reload="refresh" />
            </el-tab-pane>
            <el-tab-pane v-if="isMyTab('LingyongDZKJ')" key="LingyongDZKJ" label="易耗领用" name="LingyongDZKJ">
              <span slot="label">
                易耗领用
                <el-badge v-if="todoNum('LingyongDZKJ') > 0" :value="todoNum('LingyongDZKJ')" size="mini" class="item" />
              </span>
              <LingyongDZKJ v-if="show === 'LingyongDZKJ'" work-order-request="todo" work-order-type="LingyongDZKJ" @reload="refresh" />
            </el-tab-pane>
            <el-tab-pane v-if="isMyTab('GenghuanKJ')" key="GenghuanKJ" label="物资更换" name="GenghuanKJ">
              <span slot="label">
                物资更换
                <el-badge v-if="todoNum('GenghuanKJ') > 0" :value="todoNum('GenghuanKJ')" size="mini" class="item" />
              </span>
              <DiaoboKJ v-if="show === 'GenghuanKJ'" work-order-request="todo" work-order-type="GenghuanKJ" @reload="refresh" />
            </el-tab-pane>
          </el-tabs>

        </el-tab-pane>
      </el-tabs>
    </el-card>
  </div>
</template>

<script>
import { todoPage } from '@neu/dcam-ui/api/workOrder'
import { AUTHORITY as cardAuthority } from '@neu/dcam-ui/utils/constants'
// 物资入库（总行）
import RukuSL from '@neu/dcam-ui/views/workOrder/processTable'
// 物资入库（科技）
import RukuKJ from '@neu/dcam-ui/views/workOrder/processTable'

// 物资领用（总行）
import LingyongSL from '@neu/dcam-ui/views/workOrder/processTable'
// 物资领用（科技）
import LingyongKJ from '@neu/dcam-ui/views/workOrder/processTable'

// 物资配发（总行）
import PeifaSL from '@neu/dcam-ui/views/workOrder/processTable'

// 物资转移（总行）
import ZhuanyiSL from '@neu/dcam-ui/views/workOrder/processTable'
// 物资转移（科技）
import ZhuanyiKJ from '@neu/dcam-ui/views/workOrder/processTable'

// 物资维修（总行）
import WeixiuSL from '@neu/dcam-ui/views/workOrder/processTable'
// 物资维修（科技）
import WeixiuKJ from '@neu/dcam-ui/views/workOrder/processTable'

// 物资借用（总行）
import JieyongSL from '@neu/dcam-ui/views/workOrder/processTable'
// 物资归还（总行）
import GuihuanSL from '@neu/dcam-ui/views/workOrder/processTable'

// 物资退库（总行）
import TuikuSL from '@neu/dcam-ui/views/workOrder/processTable'
// 物资退库（科技）
import TuikuKJ from '@neu/dcam-ui/views/workOrder/processTable'

// 物资报废（总行）
import BaofeiSL from '@neu/dcam-ui/views/workOrder/processTable'
// 物资报废（科技）
import BaofeiKJ from '@neu/dcam-ui/views/workOrder/processTable'

// 物资调拨（科技）
import DiaoboKJ from '@neu/dcam-ui/views/workOrder/processTable'
// 物资调拨（总行）
import DiaoboSL from '@neu/dcam-ui/views/workOrder/processTable'

// 低值易耗品物资入库（科技）
import RukuDZKJ from '@neu/dcam-ui/views/workOrder/processTable'
// 低值易耗品物资领用（科技）
import LingyongDZKJ from '@neu/dcam-ui/views/workOrder/processTable'
// 财管资产同步
import RukuFinanceSL from '@neu/dcam-ui/views/workOrder/processTable'

export default {
  name: 'AssetTodoTaskComponent',
  components: {
    RukuSL,
    RukuKJ,
    LingyongSL,
    LingyongKJ,
    PeifaSL,
    ZhuanyiSL,
    ZhuanyiKJ,
    WeixiuSL,
    WeixiuKJ,
    JieyongSL,
    GuihuanSL,
    TuikuSL,
    TuikuKJ,
    BaofeiSL,
    BaofeiKJ,
    DiaoboKJ,
    DiaoboSL,
    RukuDZKJ,
    LingyongDZKJ,
    RukuFinanceSL
  },
  data() {
    return {
      // 当前登录的角色
      myRoles: this.$store.getters.roles || [],
      active: '',
      active2: '',
      task: []
    }
  },
  computed: {
    // 待办任务
    todoTasks: function() {
      // 判断当前用户的角色
      const roles = this.myRoles
      const shows = []
      const codes = []
      roles.forEach(element => {
        (cardAuthority[element] ? cardAuthority[element].task : []).forEach(m => {
          if (!codes.includes(m.orderType)) {
            shows.push(m)
            codes.push(m.orderType)
          }
        })
      })
      return shows
    },
    // 默认打开
    show: {
      get: function() {
        if (this.active2) {
          return this.active2
        }
        const roles = this.myRoles
        let shows = []
        roles.forEach(element => {
          const data = (cardAuthority[element] ? cardAuthority[element].task : []).filter(l => l.type === 'kj')
          if (data) {
            shows = [...shows, ...data]
          }
        })
        return shows[0] ? shows[0].orderType : ''
      },
      set: function(newVal) {
        this.active2 = newVal
      }
    },
    // 默认打开
    show2: {
      get: function() {
        if (this.active2) {
          return this.active2
        }
        const roles = this.myRoles
        let shows = []
        roles.forEach(element => {
          const data = (cardAuthority[element] ? cardAuthority[element].task : []).filter(l => l.type === 'sl')
          if (data) {
            shows = [...shows, ...data]
          }
        })
        return shows[0] ? shows[0].orderType : ''
      },
      set: function(newVal) {
        this.active2 = newVal
      }
    },
    categories: {
      get: function() {
        if (this.active) {
          return this.active
        }
        const roles = this.myRoles
        const shows = [[]]
        let ii = 0
        roles.forEach(element => {
          shows[ii] = cardAuthority[element] ? cardAuthority[element].task : []
          ii++
        })

        ii = 0
        let maxlength = 0
        let maxlengthIndex = 0
        for (;ii < shows.length; ii++) {
          if (maxlength < shows[ii].length) {
            maxlength = shows[ii].length
            maxlengthIndex = ii
          }
        }
        return shows[maxlengthIndex][0] ? shows[maxlengthIndex][0].type : ''
      },
      set: function(newVal) {
        this.active = newVal
      }
    },
    categoriesCodes: function() {
      const codes = []
      this.myRoles.forEach(element => {
        (cardAuthority[element] ? cardAuthority[element].task : []).forEach(l => {
          if (!codes.includes(l.type)) {
            codes.push(l.type)
          }
        })
      })

      return codes
    }
  },
  created() {
    this.todoTask()
  },
  methods: {
    isMyTab(taskCode) {
      return this.todoTasks.some(it => it.orderType === taskCode)
    },
    todoTask() {
      todoPage({ page: 0, size: 1000 }).then(res => {
        this.task = res.data
      })
    },
    todoNum(taskCode) {
      return this.task.filter(l => l.type === taskCode).length
    },
    refresh() {
      this.todoTask()
      this.$emit('reload')
    }
  }
}
</script>

<style lang="scss" scoped>
.card-h-80 {
    height: calc(100vh - 174px);
    overflow: auto;
}
</style>
